/**
 * 
 */
/**
 * @author shanghaitao
 *
 */
package com.starbaba.system.menu.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.service.CommonService;
import com.starbaba.core.service.CommonServiceSupport;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.system.menu.model.MenuModel;
import com.starbaba.system.menu.model.MenuTableVO;
import com.starbaba.system.menu.model.MenuVO;
import com.starbaba.system.menu.service.MenuService;

@Component(value = "MenuServiceImpl")
public class MenuServiceImpl extends CommonServiceSupport implements MenuService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5108922671733677590L;

	@Override
	@SuppressWarnings("all")
	public List<MenuTableVO> getDataList(int iDisplayStart, int iDisplayLength, MenuModel model,
			PageResponse pageResponse, CommonService server) {
		StringBuffer sf = new StringBuffer();
		sf.append("from MenuVO");
		sf.append(" where 1 = 1 ");
		String condition = bulidCondition(model, server);
		sf.append(condition);
		sf.append(" order by orderby desc");
		String hql = sf.toString();
		String countHql = "select count(*) " + hql;
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO(MenuVO.class);
		Query query = dao.getSession().createQuery(hql);
		if (iDisplayLength != 0) {
			query.setFirstResult(iDisplayStart);
			query.setMaxResults(iDisplayLength);
		}
		// addTimeParm(model,query);
		List<MenuVO> list = query.list();
		List<MenuTableVO> returnList = new ArrayList<MenuTableVO>();
		MenuTableVO itemVo;
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		Map<Long, MenuVO> map = new HashMap<Long, MenuVO>();
		for (MenuVO menu : list) {
			itemVo = new MenuTableVO();
			itemVo.setMemo(menu.getMemo());
			itemVo.setCreator(menu.getCreator());
			itemVo.setCreatetime(menu.getCreatetime());
			itemVo.setLasttime(menu.getLasttime());
			itemVo.setStatus(menu.getStatus());
			itemVo.setLasteditor(menu.getLasteditor());
			itemVo.setLname(menu.getLname());
			itemVo.setControlid(menu.getControlid());
			itemVo.setPcontrolid(menu.getPcontrolid());
			itemVo.setType(menu.getType());
			itemVo.setMenutype(menu.getMenutype());
			itemVo.setUrl(menu.getUrl());
			returnList.add(itemVo);
		}

		Query countQuery = dao.getSession().createQuery(countHql);
		Object obj = countQuery.uniqueResult();
		if (obj instanceof Long) {
			int count = ((Long) obj).intValue();

			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		}
		return returnList;
	}

	private String bulidCondition(MenuModel model, CommonService server) {
		StringBuffer condition = new StringBuffer();

		String menuname = model.getLname();
		if (!StringUtils.isEmpty(menuname)) {
			condition.append(" and lname like '%" + menuname + "%'");
		}

		String menutype = model.getMenutype();
		if (!StringUtils.isEmpty(menutype)) {
			if (Integer.valueOf(menutype) == 1) {
				condition.append(" and menutype not in(2,3)");
			} else {
				condition.append(" and menutype = " + menutype);
			}
		}

		String menustatus = model.getStatus();
		if (!StringUtils.isEmpty(menustatus)) {
			condition.append(" and status = " + menustatus);
		}

		return condition.toString();
	}

	/**
	 * 根据id跟menutype递归修改相关菜单的menutype
	 */
	@Override
	public void updateRecursion(String id, String menutype) {
		recursion(id, menutype);
	}
    
	@SuppressWarnings("all")
	public void recursion(String id, String menutype) {
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO(MenuVO.class);
		Session session = dao.getSession();
		String hql = "from MenuVO where pcontrolid='" + id + "'";
		List<MenuVO> list = session.createQuery(hql).list();
		for (MenuVO menuVO : list) {
			if (list.size() == 0) {
				break;
			}
			menuVO.setMenutype((Integer.parseInt(menutype) + 1) + "");
			session.update(menuVO);
			recursion(menuVO.getControlid(), menutype + 1);
		}
	}

	/**
	 * 获取菜单最底层
	 */
	@Override
	public int getNum() {
		Session session = DAOFactory.getInstance().createHibernateDAO(MenuVO.class).getSession();
		String l = (String) session.createQuery("select max(a.menutype) from MenuVO a ").uniqueResult();
		int max = Integer.parseInt(l);
		return max;
	}

}
